/**
 * 静态分析回归测试
 *
 * Created by nongzhizhong on 2022/08/11.
 */
const chai = require('chai');
// eslint-disable-next-line no-unused-vars
const should = chai.should();
const path = require('path');
chai.use(require('chai-http'));
const filePath = path.join(__dirname, '..', '..', '..', 'benchmark', 'source-files', 'gjb.zip');
const Administrator = require('../../../models/administrator');
const uri = require('../uri');
const benchmark = require('../../../benchmark/static-analyze/gjb.json');

module.exports = describe('/POST /project-versions/:projectVersionId/static-analyze-regression.json', function () {
  const administrator = new Administrator();
  let project = {};
  let projectVersion = {};
  let regressionVersion = {};
  let newUser;

  function url(projectVersionId) {
    return `/project-versions/${projectVersionId}/static-analyze-regression.json`;
  }
  before(function (done) {
    administrator.login()
      .then(() => administrator.createRandomTester())
      .then((tester) => {
        newUser = tester;
        return newUser.createRandomProject('c++');
      })
      .then((res) => {
        project = res;
        return project.createStaticAnalyzeVersion(
          benchmark.verified_rules,
          benchmark.files,
          newUser
        );
      })
      .then((res) => {
        projectVersion = res;
        return projectVersion.rootDirectory.staticAnalyze();
      })
      .then(() => projectVersion.project.creator.waiting4TasksOfVersionFinished(projectVersion.versionId))
      .then(() => project.createRegressionVersion(1, `res${projectVersion.versionName}`, projectVersion.masterVersionId, filePath))
      .then((res) => {
        regressionVersion = res;
      })
      .then(done)
      .catch(done);
  });
  after(function (done) {
    administrator.removeUsers([newUser.userId])
      .then(() => {
        done();
      })
      .catch(done);
  });
  // 一键回归操作
  it('it should regression successfully', function (done) {
    const { fileId } = regressionVersion.rootDirectory;
    chai.request(uri)
      .post(url(regressionVersion.versionId))
      .set('Authorization', `Bearer ${newUser.token.accessToken}`)
      .send({ fileIds: [fileId] })
      .end((err, res) => {
        res.should.have.status(202);
        regressionVersion.project.creator.waiting4TasksOfVersionFinished(regressionVersion.versionId)
          .then(() => regressionVersion.rootDirectory.getFile(fileId))
          .then((file) => {
            chai.assert.deepEqual(file.defectsCount, {});
            done();
          })
          .catch(done);
      });
  });
  // 没有选择文件一键回归失败
  it("it should fail invalid request of missing 'fileIds'", function (done) {
    chai.request(uri)
      .post(url(regressionVersion.versionId))
      .set('Authorization', `Bearer ${newUser.token.accessToken}`)
      .send({ fileIds: [] })
      .end((err, res) => {
        res.should.have.status(400);
        done();
      });
  });
  // 老版本被删除一键回归失败
  it("it should fail invalid request of missing 'original version'", function (done) {
    project.removeVersion(projectVersion.versionId)
      .then(() => {
        chai.request(uri)
          .post(url(regressionVersion.versionId))
          .set('Authorization', `Bearer ${newUser.token.accessToken}`)
          .send({ fileIds: [] })
          .end((err, res) => {
            res.should.have.status(500);
            done();
          });
      }).catch(done);
  });
});
